package cdu.sl.controller;

import cdu.sl.common.domain.Result;
import cdu.sl.common.utils.AliyunOSSOperator;
import cdu.sl.common.utils.AliyunOSSProperties;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Api(tags = "文件上传服务")
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
public class UploadController {

     private final AliyunOSSOperator aliyunOSSOperator;

    /**
     * 上传文件
     * @param file 图片
     * @return 图片url路径
     * @throws Exception
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        log.info("文件上传:{}", file.getOriginalFilename());
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传到OSS url:{}", url);
        return Result.success(url);
    }

    /**
     * 获取指定文件夹下的所有图片URL
     * @param folderPath 文件夹路径，例如 "2025/04"
     * @return 图片URL列表
     * @throws Exception 如果访问OSS出错
     */
    @GetMapping("/images")
    public Result<List<String>> listImages(@RequestParam("folderPath") String folderPath) throws Exception {
        log.info("获取文件夹 {} 中的图片列表", folderPath);
        List<String> imageUrls = aliyunOSSOperator.listImagesInFolder(folderPath);
        log.info("文件夹 {} 中找到 {} 张图片", folderPath, imageUrls.size());
        return Result.success(imageUrls);
    }



}
